# db_utils.py

from sqlalchemy import create_engine
from config import Config

# 使用配置中的 URI 创建引擎
engine = create_engine(Config.SQLALCHEMY_DATABASE_URI, echo=True)  # echo=True 可选，用于打印调试信息

def insert_cleaned_data(df, cleaned_schema):
    rename_map = {col: cleaned_col["name"] for col, cleaned_col in zip(df.columns, cleaned_schema["columns"])}
    df.rename(columns=rename_map, inplace=True)

    df.to_sql(cleaned_schema["physical_table_name"], engine, if_exists="replace", index=False)
    print(f"已创建表 {cleaned_schema['physical_table_name']}")